package com.nowcoder.community.util;

import ch.qos.logback.core.util.StringUtil;
import jakarta.annotation.PostConstruct;
import org.apache.commons.lang3.CharUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

@Component
public class SensitiveFilter {

    private static final Logger logger = LoggerFactory.getLogger(SensitiveFilter.class);

    private static final String RESLACECHARA="**";

    private TrieNode rootNode =new TrieNode();

    @PostConstruct
    public void init() {
        try (
             InputStream is = this.getClass().getClassLoader().getResourceAsStream("SensitiveWordsList.txt");
        ) {

            if (is != null) {
                try (BufferedReader reader =new BufferedReader(new InputStreamReader(is));
                ){
                    String keyword;
                    while ((keyword = reader.readLine() )!=null) {
                        this.addKeyWord(keyword);
                    }
                }
            }
        } catch (IOException e){
                logger.error("加载敏感词列表失败："+e.getMessage());
        }
    }

    private void addKeyWord(String keyword) {
        TrieNode tempNode =rootNode;
        for (int i =0;i<keyword.length();i++) {
            char c =keyword.charAt(i);
            TrieNode subNode =tempNode.getSubNode(c);

            if (subNode==null) {
                subNode=new TrieNode();
                tempNode.addSubNode(c,subNode);
            }

            tempNode=subNode;

            if ( i==keyword.length()-1) {
                tempNode.setKeyWordEnd(true);
            }
        }
    }
    private class TrieNode {

        private boolean isKeyWordEnd =false;

        private Map<Character, TrieNode>  subNode =new HashMap<>();

        public boolean isKeyWordEnd() {
            return isKeyWordEnd;
        }

        public void setKeyWordEnd(boolean keyWordEnd) {
            isKeyWordEnd = keyWordEnd;
        }

        public void addSubNode(Character c,TrieNode t) {
            subNode.put(c,t);
        }

        public TrieNode getSubNode(Character c) {
            return subNode.get(c);
        }
    }

    private boolean isSymbol(Character c) {
        return !CharUtils.isAsciiAlphanumeric(c) && (c<0X2E80 || c>0X9FFF);
    }

    public String filter(String text) {
        if (StringUtil.isNullOrEmpty(text)) {
            return null;
        }
        TrieNode temp =rootNode;
        int begin=0;
        int end=0;
        StringBuffer sb =new StringBuffer();
        char c;
        while (end<text.length()) {
            c=text.charAt(end);
            if (isSymbol(c)) {
                if (temp==rootNode) {
                    sb.append(c);
                    begin++;
                }

                end++;
                continue;
            }
            temp=temp.getSubNode(c);
            if (temp==null) {
                sb.append(text.charAt(begin));
                begin++;
                end=begin;
                temp=rootNode;
            } else if (temp.isKeyWordEnd()) {
                sb.append(RESLACECHARA);
                end++;
                begin=end;
                temp=rootNode;

            }else {
                end++;
            }

        }
        sb.append(text.substring(begin));
        return sb.toString();
    }
}
